﻿using EnvironSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace SkillSystem
{
    public  class SkillEffectProcesser:SkillItemProcesserBase
    {
        public Transform EffectParentTrans;

        public ISkillTransmitter CurSkillTransmitter;

        public Dictionary<int, GameObject> Effects = new Dictionary<int, GameObject>();

        public Action OnDamage;

        public SkillEffectProcesser(ISkillTransmitter skillTransmitter, Transform effectParentTrans)
        {
            CurSkillTransmitter = skillTransmitter;
            EffectParentTrans = effectParentTrans;
        }

        public override void Break()
        {
            OnSkillCompleted();
        }

        public override  void Do()
        {
            int id = CurSkillTransmitter.CurSkillItem.CurSkillItemData.ID;
            foreach (var item in Effects)
            {
                SkillEffectCtrlBase effectCtrl = item.Value.GetComponent<SkillEffectCtrlBase>();
                if (effectCtrl != null)
                {
                    effectCtrl.enabled = false;
                }
            }
            if (!Effects.ContainsKey(id))
            {
                GameObject prefab = Resources.Load<GameObject>(CurSkillTransmitter.CurSkillItem.CurSkillItemData.Effect);
                if (prefab != null)
                {
                    GameObject effectGo = MonoBehaviour.Instantiate(prefab, EffectParentTrans);
                    effectGo.transform.localPosition = Vector3.zero;
                    SkillEffectCtrlBase effectCtrl = effectGo.GetComponent<SkillEffectCtrlBase>();
                    if (effectCtrl!=null)
                    {
                        effectCtrl.Init(this);
                        effectCtrl.Refresh();
                        effectCtrl.enabled = true;
                        effectCtrl.RegisterDamageEvent(OnDamage);
                    }
                    else
                    {
                        DebugUtils.LogError("未绑定特效控制器");
                    }
                    Effects.Add(id, effectGo);
                }
            }
            else
            {
                SkillEffectCtrlBase effectCtrl = Effects[id].GetComponent<SkillEffectCtrlBase>();
                if (effectCtrl!=null)
                {
                    effectCtrl.enabled = true;
                    effectCtrl.Refresh();
                }
            }

            if (OnItemCompleted != null)
            {
                OnItemCompleted();
            }

            CurProcesserState = SkillProcesserState.End;
        }



        public Dictionary<AreaType, IAreaArgs> GetDamageArea()
        {
            int id = CurSkillTransmitter.CurSkillItem.CurSkillItemData.ID;
            if (Effects.ContainsKey(id))
            {
                SkillEffectCtrlBase effectCtrl = Effects[id].GetComponent<SkillEffectCtrlBase>();
                return effectCtrl.GetDamageArea();
            }
            else
            {
                DebugUtils.LogError("未加载 id为" + id + "的预制体");
            }
            return null;
            
        }

        public void RegisterDamageEvent(Action onDamage)
        {
            OnDamage += onDamage;
        }

        public override void OnSkillCompleted()
        {
            foreach (var item in Effects)
            {
                SkillEffectCtrlBase effectCtrl = item.Value.GetComponent<SkillEffectCtrlBase>();
                if (effectCtrl!=null)
                {
                    effectCtrl.enabled = false;
                }
            }
        }
    }
}

